package com.tuanzi.loan.web.bean.miniApps.articleLibrary;

import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsArticle;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsRelation;
import com.tuanzi.loan.business.service.miniApps.articleLibrary.HealthHeadlineService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.miniApps.articleLibrary.HealthHeadlineRequest;
import com.tuanzi.loan.vo.response.miniApps.articleLibrary.HealthHeadlineResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.miniApps.articleLibrary.HealthHeadlineListLazyDataModel;

/**
 * 养生头条列表
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class HealthHeadlineListBean extends JSFListingBean<HealthHeadlineRequest, HealthHeadlineResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    HealthHeadlineService healthHeadlineService;
    
    private List<MiniAppsRelation> tabList = null; // 标签列表
    private List<MiniAppsRelation> miniAppsList = null; // 小程序列表
    private List<MiniAppsArticle> sourceList = null; // 来源列表
    
    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
    	return null;
    }
    
    public String onEdit(String id) {
		return redirect("/views/miniApps/articleLibrary/health-headline-info.xhtml?id=" + id);
	}

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }

    private void initList(){
    	tabList = healthHeadlineService.getTabList();
    	miniAppsList = healthHeadlineService.getMiniAppsList();
    	sourceList = healthHeadlineService.getSourceList();
    }
    
    /**
     * 文章上线
     * @param id
     */
    public void onOnline(Long id) {
    	healthHeadlineService.onlineArticle(id);
		addSuccessfullyMessage();
	}
    
    /**
     * 删除文章和相关关联
     * @param id
     */
    public void onDeleteArticle(Long id) {
    	healthHeadlineService.deleteArticle(id);
		addSuccessfullyMessage();
    }
    
    /**
     * 文章下线
     * @param id
     */
    public void onOffline(Long id) {
    	healthHeadlineService.offlineArticle(id);
		addSuccessfullyMessage();
    }
    
    /**
     * 设置推荐状态
     */
    public void onOnlineRecommend(Long id) {
    	healthHeadlineService.saveRecommendStatus(id, 1);
    	addSuccessfullyMessage();
    }
    
    /**
     * 设置不推荐状态
     */
    public void onOfflineRecommend(Long id) {
    	healthHeadlineService.saveRecommendStatus(id, 0);
    	addSuccessfullyMessage();
    }

    public void onFlushCache(){
    	healthHeadlineService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new HealthHeadlineListLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new HealthHeadlineRequest();
        lazyDataModel = new HealthHeadlineListLazyDataModel($);
    }
    
    @Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(ONLINE_OFFLINE_STATUS);
	}
    
}




